package com.xunker.msm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.xunker.appointment.enums.EmailCodeTemplate;
import com.xunker.appointment.vo.mq.EmailVo;
import com.xunker.msm.service.MsmService;
import com.xunker.msm.utils.ConstantPropertiesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
    @Autowired
    private JavaMailSenderImpl mailSender;
    @Override
    public boolean sendPhone(String phone, String code) {
        //判断
        if (StringUtils.isEmpty(phone)) {
            return false;
        }
        //整合阿里云短信服务
        //设置相关参数
        DefaultProfile profile = DefaultProfile.
                getProfile(ConstantPropertiesUtils.REGION_Id,
                        ConstantPropertiesUtils.ACCESS_KEY_ID,
                        ConstantPropertiesUtils.SECRECT);
        IAcsClient client = new DefaultAcsClient(profile);

        //固定设置，调用阿里云接口
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //签名名称
        request.putQueryParameter("SignName", "我的项目在线学习网站");
        //模板code
        request.putQueryParameter("TemplateCode", "SMS_180051135");
        //验证码  使用json格式   {"code":"123456"}
        Map<String,Object> param = new HashMap();
        param.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean sendEmail(String email, String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        //判断
        if (StringUtils.isEmpty(email)) {
            return false;
        }
        message.setFrom(ConstantPropertiesUtils.E_USERNAME);
        message.setTo(email);
        message.setSubject(EmailCodeTemplate.E00001.getMsg());
        message.setText(code);
        mailSender.send(message);
        return true;
    }

    @Override
    public boolean sendMqEmail(EmailVo emailVo) {
        if(!StringUtils.isEmpty(emailVo.getEmail())) {
            //验证码
            //String code = (String)emailVo.getParam().get("code");
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(ConstantPropertiesUtils.E_USERNAME);
            message.setTo(emailVo.getEmail());
            message.setSubject(EmailCodeTemplate.E00002.getMsg());
            message.setText(emailVo.getParam().get("name")+"先生,你好！您于"+emailVo.getParam().get("quitTime")+"所预约的"+emailVo.getParam().get("title")+"已支付"+emailVo.getParam().get("amount")+"，请于"+emailVo.getParam().get("reserveDate")+"按时到达！");
            mailSender.send(message);
            return true;
        }
        return false;
    }
}
